package boot.spring.service.impl;

import boot.spring.entity.Module;
import boot.spring.entity.Permission;
import boot.spring.entity.PermissionsModule;
import boot.spring.mapper.ModuleMapper;
import boot.spring.mapper.PermissionMapper;
import boot.spring.service.RightsmanagementService;
import boot.spring.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class RightsmanagementServiceImpl implements RightsmanagementService {
    @Autowired/*注入模块的持久层*/
    private ModuleMapper moduleMapper;

    @Autowired/*注入权限的持久层*/
    private PermissionMapper permissionMapper;

    Result r=new Result();
    /*创建模块权限融合的实体类的集合*/
    List<PermissionsModule> ListPM=new ArrayList<>();

    @Override/*权限模块融合返回*/
    public List<PermissionsModule> ListPmSerAll() {
        ListPM.clear();
        /*获取所有的模块*/
        List<Module> Listmodules = moduleMapper.ModuleMapAll();
        /*获取所有的权限*/
        List<Permission> Listpermissions = permissionMapper.ListPerMapAll();

        Integer pmsize=0;
        for(int i=0;i<Listmodules.size();i++){
            pmsize+=1;/*作为前端的唯一值*/
            /*把模块的模块id、模块名称、模块父节点id、模块路径存到ListPM*/
            ListPM.add(new PermissionsModule(pmsize,Listmodules.get(i).getMid(),null,Listmodules.get(i).getMname(),Listmodules.get(i).getParentid(),null,Listmodules.get(i).getUrl()));
        }
        for(int i=0;i<Listpermissions.size();i++){
            pmsize+=1;/*作为前端的唯一值*/
            /*把权限的权限id、权限名称、权限的模块id、权限标识符存到ListPM*/
            ListPM.add(new PermissionsModule(pmsize,null,Listpermissions.get(i).getPid(),Listpermissions.get(i).getPname(),Listpermissions.get(i).getMname(),Listpermissions.get(i).getPidentifier(),null));
        }

        return ListPM;
    }

    /*新增权限*/
    @Override
    public Result GetPerSerAdd(Permission add){
        Integer getadd=permissionMapper.GetPerMapAdd(add);
        if (getadd>0){
            r=new Result(0,"新增成功");
        }else{
            r=new Result(1,"新增失败");
        }
        return  r;
    }

    /*删除权限*/
    @Override
    public Result GetPerSerDel(Integer pid) {
        Integer getdel=permissionMapper.GetPerMapDel(pid);
        if (getdel>0){
            r=new Result(0,"删除成功");
        }else{
            r=new Result(1,"删除失败");
        }
        return  r;
    }
}
